<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-switch flex flex-col" [style.pointer-events]="ctx.isEdit ? 'none' : 'all'" *ngIf="switchType === 'switch'; else slideToggle">
  <div #switchErrorContainer class="error-container flex flex-row items-center justify-center" [style.background]="error?.length ? 'rgba(255,255,255,0.25)' : 'none'">
    <span #switchError class="switch-error">{{ error }}</span>
  </div>
  <div #switchTitleContainer class="title-container flex max-h-30% flex-full flex-row items-center justify-center" [class.!hidden]="!showTitle">
    <span #switchTitle class="switch-title">{{title}}</span>
  </div>
  <div #switchContainer class="flex flex-full flex-col items-center justify-center"
       [class]="{
          'max-h-70%': showTitle,
          'max-h-100%': !showTitle
       }"
       id="switch-container">
    <div #switch class="switch">
      <mat-slide-toggle #matSlideToggle [(ngModel)]="value" (change)="onValue()">
      </mat-slide-toggle>
    </div>
    <div #onoffContainer class="onoff-container flex flex-row items-start justify-center" [class.!hidden]="!showOnOffLabels">
      <span #offLabel class="off-label flex-1" [class.!hidden]="value" style="text-align: center;">OFF</span>
      <span class="flex-1" [class.!hidden]="!value"></span>
      <span #onLabel class="on-label flex-1" [class.!hidden]="!value" style="text-align: center;">ON</span>
      <span class="flex-1" [class.!hidden]="value"></span>
    </div>
  </div>
  <div #textMeasure id="text-measure"></div>
</div>
<ng-template #slideToggle>
  <div class="tb-absolute-fill flex flex-col items-center justify-center" [style.pointer-events]="ctx.isEdit ? 'none' : 'all'">
    <div>
      <mat-slide-toggle [disabled]="executingUpdateValue" [labelPosition]="labelPosition" [color]="sliderColor" [(ngModel)]="value" (change)="onValue()">{{title}}</mat-slide-toggle>
      <mat-progress-bar style="height: 2px;" [color]="sliderColor" mode="indeterminate" *ngIf="executingUpdateValue"></mat-progress-bar>
      <div style="height: 2px;" *ngIf="!executingUpdateValue"></div>
      <tb-error style="margin-top: -6px; display: block; pointer-events: none;"
                [error]="error"></tb-error>
    </div>
  </div>
</ng-template>
